package com.gba.mark.user.service.user.impl;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gba.mark.user.entity.user.User;
import com.gba.mark.user.mapper.user.UserMapper;
import com.gba.mark.user.service.user.IUserService;
import com.gba.mark.user.utils.PasswordUtils;
import com.gba.mark.util.ResponseUtil;
import net.bytebuddy.implementation.Implementation;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private IUserService userService;

    @Transactional(
            rollbackFor = {Exception.class}
    )

    @Override
    public String register(String username, String phoneNum, String password, String password2) {
        // 判断两次输入的密码是否一致
        if (!password.equals(password2)) {
            return "pw_ns";
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        // 判断用户是否已经存在
        if (user == null) {
            user = new User();
            user.setUsername(username);
            user.setPassword(PasswordUtils.getSlatPwMd5(password));
            user.setPhone(phoneNum);
            user.setCreate_time(new Date());
            userService.save(user);
            return "success";
        } else {
            return "un_ex";
        }
    }

    @Override
    public String retrieve(String username, String phoneNum, String newPaswd, String newPaswd2) {

        // 判断两次输入的密码是否一致
        if (!newPaswd.equals(newPaswd2)) {
            return "pw_ns";
        }
        // 判断用户是否不存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            return "un_nex";
        }
        String phone = user.getPhone();
            if (!phone.equals(phoneNum)) {
                return "tel_nmat";
            } else {
                user.setPassword(PasswordUtils.getSlatPwMd5(newPaswd));
                userMapper.updateById(user);
                return "success";
            }
        }
    }





